package com.sduept.nwld.dataserver.controller.base.secondary;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.event.CellEditEvent;
import org.primefaces.event.SelectEvent;
import org.springframework.beans.factory.annotation.Autowired;

import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.cim.model.core.PowerSystemResource;
import com.sduept.cim.model.extend.LineSegment;
import com.sduept.cim.model.protection.ProtectionEquipment;
import com.sduept.cim.wave.service.ProtectCheckPersistManager;
import com.sduept.core.dao.SuperDAO;
import com.sduept.core.entity.IdentifiedObject;
import com.sduept.bigdata.weather.map.MapDataCache;
import com.sduept.nwld.dataserver.manager.cim.CimManager;
@Named
@ViewScoped
public class ProtectCheckDialogController extends AbstractController{
	/**
	 * 
	 */
	private static final long serialVersionUID = 4979619843390315178L;
	@Autowired
	private MapDataCache cc;
	@Autowired
	private CimManager cm;
	@Autowired
	private ProtectCheckPersistManager protectCheckPersistManager;
	@Autowired
	private SuperDAO superDao;
	private List<String> proKinds = new ArrayList<String>();
	private List<StationProDTO> currentDeviceProList = new ArrayList<StationProDTO>();//弹出框数据源
	private ProtectionEquipment selected = new ProtectionEquipment();//前台选择的
	private List<String> protectedTypes = new ArrayList<>();//一次设备类型
	
	@PostConstruct
	public void init(){
		initProKind();
		String viewType = getParameter("viewType");
		String protectedId = getParameter("protectedId");
		String stationId = getParameter("stationId");
		initData(viewType,protectedId,stationId);
		
		//初始化一次设备类型
		List<?> AllProtectedTypes = superDao
				.nativeQuery("select protected_type from t_cim_protection_equipment where protected_type is not null and protected_type !=''  group by protected_type");
		for (Object o : AllProtectedTypes) {
			protectedTypes.add(o.toString());
		}
	}
	private void initProKind(){
		proKinds.add("");
		proKinds.add("主一保护");
		proKinds.add("主二保护");
		proKinds.add("主三保护");
		proKinds.add("辅A保护");
		proKinds.add("辅B保护");
		proKinds.add("非电量保护");
		proKinds.add("短引线保护");
		proKinds.add("断路器保护");
	}
	/**
	 * 设备连接选择
	 * @param dto
	 */
	private  void initData(String viewType,String  protectedId,String stationId){
		if("line".equals(viewType)){
			LineSegment ac = cc.getLineid_map().get(protectedId);
			if(StringUtils.isNotEmpty(ac.getStartST())){
				List<ProtectionEquipment> list = cm.getProtectionEquipmentsByStationId(ac.getStartST());
				StationProDTO sdto = new StationProDTO();
				sdto.setStationName(ac.getStartStName());
				sdto.setProList(list);
				currentDeviceProList.add(sdto);
			}else{
				StationProDTO sdto = new StationProDTO();
				sdto.setStationName("无起始站");
				sdto.setProList(new ArrayList<ProtectionEquipment>());
				currentDeviceProList.add(sdto);
			}
			if(StringUtils.isNotEmpty(ac.getEndST())){
				List<ProtectionEquipment> list = cm.getProtectionEquipmentsByStationId(ac.getEndST());
				StationProDTO sdto = new StationProDTO();
				sdto.setStationName(ac.getEndStName());
				sdto.setProList(list);
				currentDeviceProList.add(sdto);
			}else{
				StationProDTO sdto = new StationProDTO();
				sdto.setStationName("无结束站");
				sdto.setProList(new ArrayList<ProtectionEquipment>());
				currentDeviceProList.add(sdto);
			}
		}else{
			List<ProtectionEquipment> list = cm.getProtectionEquipmentsByStationId(stationId);
			StationProDTO sdto = new StationProDTO();
			if(list.size()>0)
				sdto.setStationName(list.get(0).getSubName());
			sdto.setProList(list);
			currentDeviceProList.add(sdto);
		}
	}
	/**
	 * 保护修正编辑
	 * @param event
	 */
	public void onCellEdit(CellEditEvent event) {
//		ProtectionEquipment pro = null;//保存
		for (StationProDTO dto : currentDeviceProList) {
			for(ProtectionEquipment pe:dto.getProList()){
				if (pe.getId().equals(event.getRowKey())) {
//					protectCheckPersistManager.updateProtect(pro);
					protectCheckPersistManager.updateProtect(pe);
					addSuccessMessage("编辑成功！");
					break;
				}
			}
		}
	}
	
	/**
	 * 校验前台数据选择的条数
	 */
	public void checkSelected(){
		
		if(selected == null || selected.getId() == null || "".equals(selected.getId())){
			addErrorMessage("请选择要关联一次设备的数据！");
		}
	}
	
	/**
	 * 弹出设备选择器
	 */
	public void getEquipmentByStation(){
		
		Map<String,Object> options = new HashMap<String, Object>();
        options.put("resizable", true);
        options.put("draggable", true);
        options.put("modal", false);
        options.put("width", 600);
        options.put("height", 400);
        Map<String,List<String>> params=new HashMap<>();
        List<String> s=new ArrayList<>();
        s.add(selected.getSubId());
        params.put("stationId", s);
        openDialog("/ui/common/equipment_selector_single",  params,options);
	}
	
	/**
	 * 设备选择对话框回调事件
	 * @param event
	 */
	public void onEquDialogChosenListener(SelectEvent event) {
		IdentifiedObject equipment = (IdentifiedObject) event.getObject();
		if(equipment!=null && equipment instanceof PowerSystemResource){
			
			PowerSystemResource pe = (PowerSystemResource)equipment;
			selected.setProtectedId(pe.getId());
			selected.setProtectedName(pe.getName());
		}else{
			addErrorMessage("选择必须为一次设备！");
		}
	}
	
	/**
	 * 更新数据
	 */
	public void update(){
		
		if(selected != null){
			
			superDao.update(selected, ProtectionEquipment.class);
			for(StationProDTO spd : currentDeviceProList){
				
				List<ProtectionEquipment> proeList = spd.getProList();
				for(ProtectionEquipment pro : proeList){
					
					if(pro.getId().equals(selected.getId())){
						try {
							BeanUtils.copyProperties(pro, selected);
						} catch (IllegalAccessException | InvocationTargetException e) {
							e.printStackTrace();
						}
					}
				}
			}
			addSuccessMessage("保存成功！");
		}
	}
	
	public  class StationProDTO{
		private String stationName;
		private List<ProtectionEquipment> proList;
		public String getStationName() {
			return stationName;
		}
		public List<ProtectionEquipment> getProList() {
			return proList;
		}
		public void setStationName(String stationName) {
			this.stationName = stationName;
		}
		public void setProList(List<ProtectionEquipment> proList) {
			this.proList = proList;
		}
	}
	public List<StationProDTO> getCurrentDeviceProList() {
		return currentDeviceProList;
	}

	public void setCurrentDeviceProList(List<StationProDTO> currentDeviceProList) {
		this.currentDeviceProList = currentDeviceProList;
	}
	public List<String> getProKinds() {
		return proKinds;
	}
	public void setProKinds(List<String> proKinds) {
		this.proKinds = proKinds;
	}
	public ProtectionEquipment getSelected() {
		return selected;
	}
	public void setSelected(ProtectionEquipment selected) {
		this.selected = selected;
	}
	public List<String> getProtectedTypes() {
		return protectedTypes;
	}
	public void setProtectedTypes(List<String> protectedTypes) {
		this.protectedTypes = protectedTypes;
	}
}
